package com.oba.king.businessprocess.storagemanage.instorage;

/**
 * Title:           InStorageDao
 * Description:
 * Copyright:       Copyright (c) 2010
 * Company:         King Top Limited
 * @author          Wu Liang
 * @version 1.0
 *
 * MODIFICATION HISTORY
 * +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 * Date Modified        Modified By         Comments
 * +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 * 28/04/2010           Wu Liang            Code Creation
 */

import java.util.List;

import com.oba.base.dao.BaseDao;
import com.oba.base.interfaces.IBaseBo;
import com.oba.base.interfaces.IHsqlBuilder;
import com.oba.base.util.Util;
import com.oba.king.datalib.productinfo.ProductInfo;
import com.oba.king.sysmanager.Dept;

public class InStorageDao extends BaseDao {
	private final static String deleteSqlStr = "DELETE FROM INSTORAGEPRDT WHERE IN_STORAGE_ID = ";
	private final static String sqlStr = "SELECT PRODUCT_ID FROM PRODUCTINFO";	

	/**
	 * 用于查询所有入库单,以审核状态排序
	 */
	public IHsqlBuilder buildAll(){
		IHsqlBuilder sql = this.getSqlBuilder("from InStorage inSto");
		sql.addend("order by inSto.inStorageCode");
		return sql;
	}  
	public IHsqlBuilder findNeedAuditInStorage() {
		IHsqlBuilder sql = getSqlBuilder("from InStorage inSto");
		sql.add("inSto.auditStatus in (1,4)");//1 新增未审核 4修改未审核
		sql.addend("order by inSto.inStorageCode");
		return sql;
		
	}
	
	/**
	 * 查询入库产品中间表通过入库表中的
	 * @param inStorage 入库表对象，用入库表中的id进行查询
	 * @return
	 */
	public InStoragePrdt findInStoragePrdtByInStoId(InStorage inStorage) {
		IHsqlBuilder sql = getSqlBuilder("from InStoragePrdt isp");
		sql.add("isp.inStorage = ?" ,inStorage.getInStorageId());
		List<InStoragePrdt> list = find(sql);
		InStoragePrdt isp = new InStoragePrdt();
		if(list.size()>0) {
			isp = list.get(0);
//			isp.setProductInfo(this.findProductByPrdtId(isp));
			isp.setProductInfo((ProductInfo)this.load(ProductInfo.class, isp.getProductInfo().getProductId()));
			return isp;
		}
		return isp;
	} 
	
//	/**
//	 * 查询产品表通过入库产品中间表
//	 */
//	public ProductInfo findProductByPrdtId(InStoragePrdt isp) {
//		IHsqlBuilder sql = getSqlBuilder("from ProductInfo p");
//		sql.add("p.productId = ?" ,isp.getProductId());
//		List<ProductInfo> list = find(sql);
//		if(list.size()>0) {
//			return list.get(0);
//		}
//		return new ProductInfo();
//	}  
	
	/**
	 * 根据入库单号查询入库单中的详细信息
	 * @param inStorageCode 入库单号
	 * @return 返回查询的sql
	 */
	public IHsqlBuilder srchByInStorageCode(InStorage inSto){
		IHsqlBuilder sql = this.getSqlBuilder("from InStoragePrdt isp");
		sql.add("isp.inStorage = ?",inSto);
		return sql;
	}
	
	public InStorage srchInStorageById(InStoragePrdt isp) {
		IHsqlBuilder sql = this.getSqlBuilder("from InStorage inS");
		sql.add("inS.inStorageId = ?",isp.getInStorage().getInStorageId());
		List<InStorage> list = find(sql);
		InStorage is = new InStorage();
		if(list.size()>0) {
			is = list.get(0);
			return is;
		}
		return is;
	} 
	
	public ProductInfo srchPrdtById(InStoragePrdt isp) {
		IHsqlBuilder sql = this.getSqlBuilder("from ProductInfo prdt");
		sql.add("prdt.productId = ?",isp.getProductInfo().getProductId());
		List<ProductInfo> list = find(sql);
		ProductInfo is = new ProductInfo();
		if(list.size()>0) {
			is = list.get(0);
			return is;
		}
		return is;
	} 
	
	/**
	 * 删除
	 */
	public void deleteInStorePrdt(InStorage is){//inStorageCode
		jdao.execute(deleteSqlStr+is.getInStorageId());
		delete(is);
	}
	
	/**
	 * 查询部门，当新增入库单中的信息时需要增加部门信息
	 * @return 返回部门信息
	 */
	public List<Dept> buildAllDept() {
		return find("from Dept dept");
	}
	
	/**
	 * 将所有药品信息查询出来
	 * TODO 可以将一个入库单中的产品排除
	 * @return
	 */
	public IHsqlBuilder srchPrdtForChoose(List<IBaseBo> list){
		String idsStr ="";
		if(list.size()!=0){
			idsStr = Util.idsFormatByBo(list);			
		}
		IHsqlBuilder sql = this.getSqlBuilder("from ProductInfo prdt");
		if(idsStr!=null && !idsStr.trim().equals("")){
			sql.add("prdt.productId not in "+idsStr);
		}		
		sql.addend("order by prdt.productName");
		return sql;
	}
	
	public List<InStoragePrdt> srchInStoragePrdt(InStorage inSto){
		IHsqlBuilder sql = this.getSqlBuilder("from InStoragePrdt isp");
		sql.add("isp.inStorage = ?",inSto);
		return find(sql);
	}	
	/**
	 * 入库单的模糊查询
	 * @param inStorageCondition 查询条件
	 * @return
	 */
	public IHsqlBuilder srchByInStorageCondition(InStorageCondition inStorageCondition){
		IHsqlBuilder sql = getSqlBuilder("from InStorage inSto");
		if(inStorageCondition.getInStorageCode()!=null&& !inStorageCondition.getInStorageCode().trim().equals("")){
			sql.add("inSto.inStorageCode =?", inStorageCondition.getInStorageCode().toString().trim());			
		} 
		if(inStorageCondition.getResponMan()!=null && !inStorageCondition.getResponMan().trim().equals("")){
			sql.add("inSto.responMan like ?", inStorageCondition.getResponMan().trim(),"%","%");			
		}
		if(inStorageCondition.getInDate()!=null && !inStorageCondition.getInDate().equals("")){
			sql.add("inSto.inDate > ?", inStorageCondition.getInDate());			
		}
		if(inStorageCondition.getEndDate()!=null && !inStorageCondition.getEndDate().equals("")){
			sql.add("inSto.inDate < ?", inStorageCondition.getEndDate());			
		}
		if(inStorageCondition.getAuditStatus()!=null && !inStorageCondition.getAuditStatus().equals("")&&!"0".equals(inStorageCondition.getAuditStatus())){
			sql.add("inSto.auditStatus = ?", inStorageCondition.getAuditStatus());			
		}
		sql.addend("order by inSto.inStorageCode");
		return sql;
	}
	
	/**
	 * 查询指定药品增加到入库单中
	 * @param productSerialId 药品编号
	 * @param productName 药品名称
	 * @return
	 */
	public IHsqlBuilder srchPrdtForChoose(List<Long> productIdList, InStorageCondition isc){
		String idsStr = Util.idsFormat(productIdList);	
		IHsqlBuilder sql = this.getSqlBuilder("from ProductInfo prdt");
		sql.add("prdt.productSerialId = ?",isc.getProductSerialId());
		sql.add("prdt.productName like ?",isc.getProductName(),"%","%");		
		sql.add("prdt.productId not in "+idsStr);
		sql.addend("order by prdt.productName");
		return sql;
	}
	
}
